<?php
/* 用户表 */
namespace app\msg\model;


use think\Db;
use think\Model;

class MessageTargetUser extends Model {


    protected $tableName = 'message_target_user';
    
    const MESSAGE_STATUS_DRAFT=1;//存草稿箱
    const MESSAGE_STATUS_OK=2;//发布状态
    const MESSAGE_STATUS_DELETE=3;//删除
    

    public function getOneMessageBoxDataByID($id)
    {
        $result = Db::name($this->tableName)
                    ->where('id ='.$id)
                    ->find();

        return $result;
    }



    public function insertManyData($data)
    {
//        $result = Db::name($this->tableName)->insertAll($data);
//        return $result;
    
        $rule = [
            'type' => 'month', // 分表方式
            'expr'=>0,
        ];
        $result = Db::name($this->tableName)->partition(['Ym' => time()], "Ym", $rule)->insertAll($data);
        return $result;
    }
    
    /*
     * 通过消息ID删除记录
     */
    public function deleteMessageTargetUserByMsgID($msgId,$addTime){
        $rule = [
            'type' => 'month', // 分表方式
            'expr'=>0,
        ];
        return Db::name($this->tableName)->partition(['Ym' => $addTime], "Ym", $rule)->where(['m_b_id'=>$msgId])->delete();
    }
    
    
    /*
     *
     */
    public function getMessageTargetUserByMsgID($msgID) {
        if(!(int)$msgID)return false;
        $msgData=Db::name('message_box')->field(['add_time'])->where(['id'=>$msgID])->find();
        if(!$msgData){
            return false;
        }
    
        $rule = [
            'type' => 'month', // 分表方式
            'expr'=>0,
        ];
        $data=Db::name($this->tableName)
          ->partition(['Ym' => $msgData['add_time']], "Ym", $rule)
            ->field('customer_id')
          ->where(['m_b_id'=>$msgID])
          ->select();
        $idString='';
        if(!empty($data)){
            foreach ($data as $v){
                $idString.=$v['customer_id'].',';
            }
            $idString=rtrim($idString,',');
        }
        return $idString;
    }
    
}